import pandas
import yaml
from setting import DIR_NAME


def read_data(filepath, sheet_name):
    # keep_default_na 读取excel出现空的单元格 N/A
    # 获取不到有效值，希望读取是空字符 参数值False
    res = pandas.read_excel(DIR_NAME + filepath, sheet_name, keep_default_na=False, engine='openpyxl')
    # 获取单元格数据 传入行和列的索引
    # print(res.iloc[1, 2])
    # 数据解析不算表头，所以行从第二行开始计算
    # 获取总行数和总列数  返回元组
    # print(res.shape)
    # 总行数
    lines_count = res.shape[0]
    # 总列数
    col_count = res.shape[1]
    # 将excel文件的解析  [[],[],[]]
    # 定义列表 存放每行数据
    data = []
    for i in range(lines_count):
        # 定义列表 存放每个单元格数据 -- 每条测试用例数据
        line = []
        for j in range(col_count):
            # 构建单元格数据 表示获取第i行的 第j列
            cell_data = res.iloc[i, j]
            line.append(cell_data)
        data.append(line)
    return data


# yaml 读取
def load_yaml_file(yaml_file):
    with open(DIR_NAME + yaml_file, mode='r', encoding='UTF-8') as f:
        # 加载整个yaml文件
        yaml_content = yaml.load(f, Loader=yaml.FullLoader)
    return yaml_content


# yaml写入
def write_yaml(filepath, content):
    with open(DIR_NAME + filepath, mode='w', encoding='UTF-8') as f:
        # dumper处理输出格式组件，Python对象处理yaml格式
        yaml.dump(content, f, Dumper=yaml.Dumper)
if __name__ == '__main__':
#     # print(load_yaml_file(r'\config\http.yml')['buyer'])
    print(read_data(r'\data\mtxshop_testdata.xlsx', '立即购买'))
#     print(load_yaml_file(r'\config\common.yml')['web_TestUser'])
#     # print(load_yaml_file(r'\config\common.yml')['buyerPassword'])
#     # print(load_yaml_file(r'\config\redis_util.yml')['mtxshop'])
#     print(load_yaml_file(r'\config\common.yml')['buyerName'])
#     print(load_yaml_file(r'\config\common.yml')['buyerPassword'])
#     print(load_yaml_file(r'\config\env_test.yml'))
